#!/bin/bash 

#  Script to install the driver into the /lib/modules tree for SuSE distros. 
#  By default new drivers will be installed for the currently running kernel.
#  This behavior can be modified by specifying a specific kernel version to
#  upgrade.

#
# Copyright © 2006-2008 Ciprico Inc. All rights reserved.
# Copyright © 2008-2013 Dot Hill Systems Corp. All rights reserved.
#
# Use of this software is subject to the terms and conditions of the written
# software license agreement between you and DHS (the "License"),  
# including, without limitation, the following (as further elaborated in the
# License):  (i) THIS SOFTWARE IS PROVIDED "AS IS", AND DHS DISCLAIMS
# ANY AND ALL WARRANTIES OF ANY KIND, WHETHER EXPRESS, IMPLIED, STATUTORY,
# BY CONDUCT, OR OTHERWISE; (ii) this software may be used only in connection
# with the integrated circuit product and storage software with which it was
# designed to be used; (iii) this source code is the confidential information
# of DHS and may not be disclosed to any third party; and (iv) you may not
# make any modification or take any action that would cause this software,
# or any other Dot Hill software, to fall under any GPL license or any other
# open source license.
#

drv_basename=rcraid
kernel=
sdk=

USE_SWL="ahci,lsi1068,lsi2008"
KERNEL_CFG=/etc/sysconfig/kernel
MODULES_CFG=/etc/modprobe.conf.local

source ./common_shell

# Restore all of the system files from the backups
error_exit() {
    if [ -f ${KERNEL_CFG}.bak ] ; then
	mv -f ${KERNEL_CFG}.bak $KERNEL_CFG
    fi

    if [ -f ${MODULES_CFG}.bak ] ; then
        mv -f ${MODULES_CFG}.bak $MODULES_CFG
    fi

    rm -rf /tmp/install
    exit 1
}

while [ $# -gt 0 ] ; do
    case $1 in
        --help)
            usage_suse $0
            ;;
        USE_SWL=*)
            USE_SWL=${1:8}
            ;;
        *)
            if [ -z $kernel ] ; then
                kernel=$1
            else
                usage_suse $0
            fi
            ;;
    esac
    shift
done

# replace ,'s with spaces and remove "none"
echo "SWL option: \"${USE_SWL}\""
USE_SWL=${USE_SWL//,/ }
USE_SWL=${USE_SWL//none/ }
for swl in ${USE_SWL}; do
    if lists_intersect $swl "${SWL_SUPPORTED}"; then
        echo "$swl is not a supported SWL option"
        usage_suse $0
        exit 1
    fi
done

if [ $UID != 0 ] ; then
    echo "This script must be run as root"
    exit 1
fi

if [ ! -f /etc/SuSE-release ] ; then
    echo "This driver disk is for SuSE Linux."
    exit 1
fi

suse_release=`awk '/^VERSION/ { print $3 }' /etc/SuSE-release`
running_kernel=`uname -r`
arch=`uname -i`

if [ -z $kernel ] ; then
    kernel=$running_kernel
fi

if grep "Enterprise Server" /etc/SuSE-release ; then
    suse_release=sles${suse_release}
fi

if grep "Enterprise Desktop" /etc/SuSE-release ; then
    suse_release=sled${suse_release}
fi

if [ ! -d linux/suse ] ; then 
    if [ ! -f Makefile ]; then
        echo "Either this is not a driver diskette or sdk or you need to"
        echo "cd to the directory where the disk is mounted"
        exit 1
    else
        sdk="true"
    fi
fi

driver_path=lib/modules/${kernel}/kernel/drivers/scsi

if [ -z ${sdk} ]; then
    install_dir=linux/suse/${arch}-${suse_release}/install
    if [ ! -f ${install_dir}/update.tar.gz ] ; then
        echo "This disk does not contain any drivers for SuSE Release ${suse_release} on $(arch)"
        exit 1
    fi

    mkdir /tmp/install
    cp ${install_dir}/update.tar.gz /tmp/install

    # Check for a driver to match the current kernel configuration
    zcat /tmp/install/update.tar.gz | tar -tf - $driver_path/$drv_basename/$drv_basename.ko >& /dev/null
    if [ $? -ne 0 ] ; then
        echo "This disk does not contain drivers for kernel release ${kernel}"
        exit 1
    fi

    # Extract the driver
    zcat /tmp/install/update.tar.gz | tar -C / -xf - $driver_path/$drv_basename/$drv_basename.ko >& /dev/null
    if [ $? -ne 0 ] ; then
        echo " Couldn't extract ${drv_basename}"
        exit 1
    fi
else
    if [ ! -f $drv_basename.ko ]; then
        echo "Driver $drv_basename.ko not built. "
        echo "run: \"(cd ../; sh ./install [kernel [flavors]])\" ."
        exit 1
    fi
    # Prepend / to driver_path since we are installing at runtime
    mkdir -p /$driver_path/$drv_basename
    cp -p $drv_basename.ko /$driver_path/$drv_basename/$drv_basename.ko
fi

# Add module to the kernel configuration file
# It is first removed, then added because the value of USE_SWL may have
# changed which affects the module ordering (in cases of reinstalls).
del_initrd_mod_suse $drv_basename $KERNEL_CFG
add_initrd_mod_suse $drv_basename $KERNEL_CFG "${USE_SWL}"

# Add driver to modules configuration file
del_module_config $drv_basename $MODULES_CFG
add_module_config $drv_basename $MODULES_CFG "${USE_SWL}"

# Possibly add rcraid "use_swl=X" option
add_swl_option $drv_basename $MODULES_CFG "${USE_SWL}"

/sbin/depmod ${kernel} -a
if [ $? -ne 0 ] ; then
    echo "Failure running depmod"
    error_exit
fi

initrd="/boot/initrd-${kernel}"
if [ -e $initrd ]; then
    cp -af $initrd $initrd.old || {
	echo "couldn't save a copy of $initrd as $initrd.old... giving up"
	error_exit
    }
    didinitrd=1
else
    didinitrd=0
fi
# can't write to initrd.new because mkinitrd updates the symlink if appropriate
/sbin/mkinitrd -k "vmlinuz-${kernel}" -i $initrd
if [ $? -ne 0 ] ; then
    echo "Failed to build initrd"
    # in case the initrd got botched
    test $didinitrd = 1 && cp -a $initrd.old $initrd
    error_exit
fi

rm -f ${KERNEL_CFG}.bak 
rm -f ${MODULES_CFG}.bak
rm -rf /tmp/install
echo
echo "Installed $drv_basename driver for ${kernel}"
